<template>
  <div class="app-container">
    <!-- 员工离职页面 -->

    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="empNumber">
        <el-input
          v-model="queryParams.empNumber"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>



      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="medium"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['Empment:employedoperate:update']"
          >进行离职操作</el-button>
        </el-col>
      </el-row>


    </el-form>




    <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工id号" align="center" prop="userId" />
      <el-table-column label="员工名称" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="empNumber" />
      <el-table-column label="原部门" align="center" prop="dept.deptName" />
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" prop="idCard">
        <template slot-scope="scope">
          <span>{{ scope.row.idCard || 'N/A' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号地域" align="center" prop="idNumberRegion" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="地域" align="center" prop="region" />
      <el-table-column label="合同签订时间" align="center" prop="contractDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同截至日期" align="center" prop="contractLong" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractLong, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职类型" align="center" prop="typeOfEmployment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type_of_employment" :value="scope.row.typeOfEmployment" />
        </template>
      </el-table-column>
      <el-table-column label="人员类型" align="center" prop="typeOfMen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type_of_men" :value="scope.row.typeOfMen" />
        </template>
      </el-table-column>
      <el-table-column label="婚姻状态" align="center" prop="maritalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.marital_status" :value="scope.row.maritalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="politicalLandscape" />
      <el-table-column label="住址" align="center" prop="address" />
      <el-table-column label="最高学历" align="center" prop="highEducationalLevel" />
      <el-table-column label="毕业学校" align="center" prop="school" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="紧急联系人" align="center" prop="emergencyContact" />
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyContactPhone" />
      <el-table-column label="第一学历" align="center" prop="firstDegree" />
      <el-table-column label="第二学历" align="center" prop="secondDegree" />
      <el-table-column label="学制类型" align="center" prop="typeEducation" />
      <el-table-column label="开户行" align="center" prop="bank" />
      <el-table-column label="银行账号" align="center" prop="bankAccountNumber" />
      <el-table-column label="爱好特长" align="center" prop="hobby" />
      <el-table-column label="是否安排寝室" align="center" prop="dormitoryNeed" />
      <el-table-column label="寝室号" align="center" prop="dormitory" />
      <el-table-column label="出生年月" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="预留字段1" align="center" prop="Reserved1" />
      <el-table-column label="预留字段2" align="center" prop="Reserved2" />
      <el-table-column label="预留字段3" align="center" prop="Reserved3" />
      <el-table-column label="预留字段4" align="center" prop="Reserved4" />
      <el-table-column label="预留字段5" align="center" prop="Reserved5" />
      <el-table-column label="预留字段6" align="center" prop="Reserved6" />
      <el-table-column label="预留字段7" align="center" prop="Reserved7" />
      <el-table-column label="预留字段8" align="center" prop="Reserved8" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改离职记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-form-item label="员工id号" prop="userId">
          <el-input v-model="form.userId"  disabled placeholder="请输入员工的id号" />
        </el-form-item>
        <el-form-item label="员工名称" prop="name">
          <el-input v-model="form.name" disabled  placeholder="请输入员工名称" />
        </el-form-item>
        <el-form-item label="离职原因" prop="reasonForLeaving">
          <el-input v-model="form.reasonForLeaving" placeholder="请输入离职原因" />
        </el-form-item>
        <el-form-item label="离职情况" prop="resignationSituation">
          <el-input v-model="form.resignationSituation" placeholder="请输入离职情况" />
        </el-form-item>
        <el-form-item label="离职备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入离职备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { operatelist, updateoperate } from "@/api/Empment/employed";

export default {
  name: "employedoperate",
  dicts: ['marital_status', 'type_of_employment', 'type_of_men', 'sys_user_sex'],
  data() {
    return {
      tableData: [],
      loading: false,

      // 是否显示弹出层
      open: false,
     //被选中的id
      selectedUserIds: [],
      name:[],
      ids: [],
      // 非单个禁用
      single: true,

      deptName: undefined,

      deptOptions: undefined,
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        deptId: null,
        empNumber: null,
        sex: null,
        idCard: null,
        idNumberRegion: null,
        phoneNumber: null,
        region: null,
        contractDate: null,
        contractLong: null,
        date: null,
        typeOfEmployment: null,
        typeOfMen: null,
        maritalStatus: null,
        politicalLandscape: null,
        address: null,
        highEducationalLevel: null,
        school: null,
        major: null,
        nation: null,
        emergencyContact: null,
        emergencyContactPhone: null,
        firstDegree: null,
        secondDegree: null,
        typeEducation: null,
        bank: null,
        bankAccountNumber: null,
        hobby: null,
        dormitoryNeed: null,
        dormitory: null,
        birthday: null,
        other: null,
        Reserved1: null,
        Reserved2: null,
        Reserved3: null,
        Reserved4: null,
        Reserved5: null,
        Reserved6: null,
        Reserved7: null,
        Reserved8: null
      },
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },

  methods: {
    getList() {
      this.fetchData(); // 组件挂载后加载数据
    },

    fetchData() {
      this.loading = true;

      // 将查询参数传递给后端
      const params = {
        ...this.queryParams,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize
      };

      // 调用API接口进行数据获取
      operatelist(params)
        .then(response => {
          this.tableData = response.rows; // 从响应中获取数据并赋值给tableData
          this.total = response.total;
          this.loading = false;
        })
        .catch(error => {
          console.error("加载数据时出现错误:", error);
          this.loading = false;
        });
    },

    handleSelectionChange(selection) {
      this.selectedUserIds = selection.map(item => item.userId);
      this.name=selection.map(item => item.name);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    handleQuery() {
      this.queryParams.pageNum = 1; // 搜索时从第一页开始
      this.getList(); // 发起查询
    },

    // 表单重置
    resetQuery() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        deptName: null,
        post: null,
        entryDate: null,
        resignationDate: null,
        idCard: null,
        birthday: null,
        age: null,
        sex: null,
        highEducationalLevel: null,
        phoneNumber: null,
        typeOfMen: null,
        reasonForLeaving: null,
        resignationSituation: null,
        dormitoryNeed: null,
        notes: null
      };
      this.resetForm("form");
    },

    /** 离职按钮操作 */
    handleUpdate(row) {
      this.resetQuery();
      this.form.userId = this.selectedUserIds[0]; // 取单个ID
      this.form.name = this.name[0]; // 取单个名字
      this.open = true;
      this.title = "添加离职记录";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateoperate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },


  },

  mounted() {
    this.fetchData(); // 组件挂载后加载数据
  }

};
</script>
